// pages/login/login.js
import {request, requestWrap} from "../../request/index.js"
// import {login, getUserProfile} from "../../utils/asyncWx.js";
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    defaultAvatar: '',
    avatarUrl: '',
    isLogin: '',
    sendText: '获取验证码',
    siteUrl: '',
  },
  userInfo: '',
  nickname: '',
  name: '',
  avatar: '',
  phone: '',
  code: '',
  count: 60,
  btnFlag: true,
  isFlag: true,
  files: '',
  page: '',
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // this.page = options.page
    this.setData({
      siteUrl: app.globalData.siteUrl
    })
    this.userInfo = app.getUserInfo()
    // this.getOssParams()
    // console.log(res)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  sendCode () {
    if (this.count == 60) {
      this.sendRequst()
    } 
  },
  countDown () {
    // console.log(this.count)
    if (this.count > 0) {
      this.count -= 1
      this.setData({
        sendText: this.count + '秒'
      })
      setTimeout(() => {
        this.countDown()
      }, 1000)
    } else {
      this.count = 60
      this.setData({
        sendText: '获取验证码'
      })
    }
  },
  async sendRequst () {
    if (!this.checkPhone()) return
    let res = requestWrap(await request({
      url:"api/user/Verification_code/send",
      method:'GET',
      data: {
        username: this.phone
      }
    }))
    this.countDown()
  },
  checkPhone () {
    let reg = /^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$/
    if (!this.phone || this.phone && !this.phone.trim()) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return false
    }
    if (!reg.test(this.phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      })
      return false
    }
    return true
  },
  onChooseAvatar(e) {
    // console.log(e.detail)
    const { avatarUrl } = e.detail
    // this.setData({
    //   avatarUrl
    // })
    this.upload(avatarUrl)
  },
  onchange (e) {
    // console.log(e)
    if (e.detail.value) {
      this.nickname = e.detail.value
      this.setData({
        nickname: e.detail.value
      })
    }
  },
  handleInput (e) {
    let name = e.currentTarget.dataset.name
    const {value} = e.detail
    this[name] = value
  },
  async onSubmit () {
    if (!this.btnFlag) return
    // if (!this.headimgurl) {
    //   wx.showToast({
    //     title: '请上传头像',
    //     icon: 'none'
    //   })
    //   return
    // }
    // if (!this.nickname.trim()) {
    //   wx.showToast({
    //     title: '请输入昵称',
    //     icon: 'none'
    //   })
    //   return
    // }
    if (!this.name.trim()) {
        wx.showToast({
          title: '请输入真实姓名',
          icon: 'none'
        })
        return
      }
    if (!this.checkPhone()) return
    if (!this.code.trim()) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
      return
    }
    let postData = {}
    // postData.nickname = this.nickname
    postData.name = this.name
    // postData.headimg = this.headimgurl
    postData.phone = this.phone
    postData.code = this.code
    // console.log(postData)
    // return
    this.btnFlag = false
    let res = requestWrap(await request({
      url:"api/wxapp/user/complete",
      method:'GET',
      data: postData
    }))
    if (res) {
        wx.showToast({
            title: res.msg,
            icon: 'none'
        })
        if (res.code == 1) {
            let userInfo = wx.getStorageSync('userInfo')
            userInfo.real_name = this.name
            userInfo.mobile = this.phone
            userInfo.is_complete = 1
            wx.setStorageSync('userInfo', userInfo)
            wx.navigateBack()
        } else if (res.code == 0) {
            wx.navigateBack()
        }
    }
    this.btnFlag = true
  }
})